/**
 * 公告列表js
 * @author linfeng
 * @since 2020/12/08 15:00
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'zmnBind'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    let zmnBind = layui.zmnBind;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        defaultToolbar: [],
        height: 'full-160'
    });
    laypage.render(layui.$.extend({limits: [10, 20, 30, 40, 50, 100]}, zmnPage.laypageOptions(json)));

    // 组织架构的页面domID
    let orgInputIds = ['#plat', '#orgTwoId', '#orgId'];

    // 部门
    let bindDepts = function () {
        zmnBind.selectl(form, json.ZMN_URL_ENV + "mcc.xiujiadian.com" + '/common/dept/listChilds', 'deptOneId', 'deptTwoId', 'deptId', [json.deptOneId, json.deptTwoId, json.deptId], [function () {
            return {orgId: findInputValue(orgInputIds), parentId: 1}
        }, function () {
            return {orgId: findInputValue(orgInputIds), parentId: zmnUtil.emptyToNull($("#deptOneId").val())}
        }, function () {
            return {orgId: findInputValue(orgInputIds), parentId: zmnUtil.emptyToNull($("#deptTwoId").val())}
        }], null, false);
    };

    // 组织架构
    zmnBind.selectl(form, json.ZMN_URL_ENV + "mcc.xiujiadian.com" + '/common/organize/listChilds', 'plat', 'orgTwoId', 'orgId', [json.plat, json.orgTwoId, json.orgId], [function () {
        return {parentId: 1}
    }, function () {
        return {parentId: zmnUtil.emptyToNull($("#plat").val())}
    }, function () {
        return {parentId: zmnUtil.emptyToNull($("#orgTwoId").val())}
    }], [
        function () {
            bindDepts();
        },
        function () {
            bindDepts();
        },
        function () {
            bindDepts();
        }
    ], null, false);

    bindDepts();

    //查询条件重置
    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-notice-filter", {
            "plat": "" // "name": "value"
            ,"orgTwoId": ""
            ,"orgId": ''
            ,"deptOneId": ""
            ,"deptTwoId": ""
            ,"deptId": ""
        });
        return false;
    });


    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.relateId);
    });

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        //新增，弹出框
        if (obj.event == "add") {
            return zmnLayUtil.openIframeCustom(layer, "/notice/detail.action", {
                title: "新增公告",
                shade: 0.3,
                area: ["900px", "430px"]
            });
        }
        //启用停用
        if (obj.event == "start" || obj.event == "stop") {
            if (ids.length === 0) {
                return layer.alert("请选择至少一条公告！");
            }
            zmnPage.layStartStopBatch(obj, "/notice/updateStatus.action", ids.join(","));
        }
    });

    form.on("switch(status)", function (obj) {
        let url = "/notice/updateStatus.action";
        let id = obj.elem.getAttribute("key");
        let status = obj.elem.checked ? 2 : 1;
        switch (status) {
            case 1:
                zmnPage.ajaxSimple(url, {ids: id, status: 1}, 1000);
                break;
            case 2:
                zmnPage.ajaxSimple(url, {ids: id, status: 2}, 1000);
                break;
        }
    });

    //对外暴露的接口
    exports('notice_list_js', {});
});

//显示描述
function show_tips(flag, tips, obj) {
    //小tips
    //如果没有权限则提示
    if (!flag) {
        layer.tips(tips, obj, {
            tips: [1, '#3595CC'],
            time: 3000
        })
    }
}

/**
 * 从多个input中获取input的值，以最后一个input优先
 * @param selectors
 * @returns {number|jQuery}
 */
function findInputValue(selectors) {
    if (!zmnUtil.isArray(selectors)) {
        return 0;
    }
    let l = selectors.length;
    for (let i = l - 1; i >= 0; i--) {
        let $this = $(selectors[i]);
        if ($this.length > 0 && zmnUtil.isNotZeroInteger($this.val())) {
            return $this.val();
        }
    }
    return 0;
}

function modifyNotice(relateId) {
    return zmnLayUtil.openIframeCustom(layer, "/notice/detail.action?relateId="+relateId, {
        title: "修改公告",
        shade: 0.3,
        area: ["900px", "430px"]
    });
}

function deleteNotice(relateId) {
    layer.confirm('您确定要删除吗？', {
        btn: ['确定', '取消']
    }, function () {
        zmnPage.ajaxSimple("/notice/delete.action", {id: relateId}, 1000);
    });
}


